using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Common;
using FilteredStatistics.Common.Controller;
using FilteredStatistics.View;

namespace FilteredStatistics.Data
{
    class TemplateRecordedLapsFilterCriteria : FilterCriteriaFacade, ISplitsCriteria
    {
        public TemplateRecordedLapsFilterCriteria(IActivity activity)
        {
            Init(activity);
        }

#region FilterCriteriaFacade Members

        public override Guid ReferenceId
        {
            get { return GUIDs.TemplateSplitsCriteriaGuid; }
        }

        public override IActivity Activity
        {
            set
            {
                if (m_Activity != value)
                {
                    m_Activity = value;

                    foreach (AllActiveOrRestLapsNamedZone namedZone in NamedZones)
                    {
                        namedZone.Activity = value;
                    }
                }
            }
        }

        public override String DisplayName
        {
            get { return PluginView.GetLocalizedString("RecordedLapsText"); }
        }

        public override bool IsTemplateOnly
        {
            get { return true; }
        }

        public override bool IsMainViewOnly
        {
            get { return false; }
        }

        public override List<object> NamedZones
        {
            get { return m_SplitsNamedZones; }
        }

        public override bool IsSerializable
        {
            get { return true; }
        }

        public override UInt16 DataVersion
        {
            get { return Constants.CurrentVersion.VersionNumber; }
        }

        public override void SerializeCriteria(Stream stream)
        {
            Serialize(stream);
        }

        public override object DeserializeCriteria(Stream stream, UInt16 version)
        {
            Deserialize(stream, new DataVersion(version));

            return this;
        }

        public override object TemplateCompatibleCriteria
        {
            get
            {
                return new TemplateRecordedLapsFilterCriteria(m_Activity);
            }
        }

#endregion

#region ISplitsCriteria Members

        public SplitsCriteriaType SplitsType
        {
            get { return SplitsCriteriaType.RecordedLaps; }
        }

#endregion

#region IPluginSerializable members

        public override void Serialize(Stream stream)
        {
            base.Serialize(stream);

            SerializableInt32Range splitsType = new SerializableInt32Range((Int32)SplitsCriteriaType.RecordedLaps);

            splitsType.Serialize(stream);
        }

#endregion

#region Deserialization methods

        public void Deserialize_V1(Stream stream, DataVersion version)
        {
        }

#endregion

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            TemplateRecordedLapsFilterCriteria criteria = obj as TemplateRecordedLapsFilterCriteria;
            SplitsPlaceholderFilterCriteria criteriaPlaceholder = obj as SplitsPlaceholderFilterCriteria;

            if (criteria != null || criteriaPlaceholder != null)
            {
                return true;
            }

            return base.Equals(obj);
        }

        public void Init(IActivity activity)
        {
            m_SplitsNamedZones.Clear();
            m_SplitsNamedZones.Add(new AllActiveOrRestLapsNamedZone(activity, false));
            m_SplitsNamedZones.Add(new AllActiveOrRestLapsNamedZone(activity, true));

            Activity = activity;

            TriggerNamedZonesListChanged();
        }

        private IActivity m_Activity;
        private List<object> m_SplitsNamedZones = new List<object>();
    }
}
